import ApiFetchers from "@/apis/api_fetchers"
import { useCallback, useEffect, useState } from "react"

const getFinanceChannelList = ApiFetchers.refGetFinanceChannelList

export type ChannelType = FinanceChannelApiTypes.Fetcher.Response.record

export default function useWithdrawChannels() {
    const [channels, setChannels] = useState<ChannelType[]>()
    const [loading, setLoading] = useState(false)
    const [error, setError] = useState<string | null>(null)

    const walletChannelList = useCallback(async () => {
        try {
            setLoading(true)
            const res = await getFinanceChannelList({ mode: "2" })
            setLoading(false)

            setChannels(res?.data || [])
        } catch (error) {
            setError(error instanceof Error ? error.message : "get wallet Channel list failed")
        } finally {
            setLoading(false)
        }
    }, [])

    // 组件挂载时自动加载数据
    useEffect(() => {
        walletChannelList()
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    return {
        data: channels,
        loading: loading,
        error: error,
        refresh: walletChannelList,
    }
}
